#ifndef GITLOGPREVIEWWIDGET_H
#define GITLOGPREVIEWWIDGET_H

#include <QProcess>
#include <QWidget>
#include <functional>

#include "columnsentry.h"

class QTableWidgetItem;
// typedef std::function<Columns()> GetColumnsFunc;
using GetColumnsFunc = std::function<Columns(QTableWidgetItem *item)>;

class QProcess;
class QTableWidget;
class QVBoxLayout;
class QFrame;
class QLabel;
class QPushButton;
class QTextBrowser;
class QHBoxLayout;
namespace Ui {
class GitLogPreviewWidget;
}
class GitLogPreviewWidget : public QWidget
{
    Q_OBJECT

public:
    explicit GitLogPreviewWidget(QWidget *parent = nullptr);
    ~GitLogPreviewWidget();

    /**
     * @brief 设置 QTableWidget 实例
     * @param newInstance
     */
    void setInstance(QTableWidget *newInstance);
    /**
     * @brief 设置获取 Columns 的回调函数
     * @param newColumnsFunc
     */
    void setColumnsFunc(GetColumnsFunc newColumnsFunc);

    void setDisplayContent(const QString &commitHash, const QString &author, const QString &date,
                           const QString &message, const QString &changedFiles);

private slots:
    void showAtPosition(const QPoint &pos, QTableWidgetItem *item);
    void fetchGitLog(const QString &workdir);
    void onGitProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);
    void onCopyHashClicked();

private:
    void parserOutput(const QString &output);
    void adjustDisplayPosition(QPoint &pos);

    // QObject interface
public:
    bool eventFilter(QObject *watched, QEvent *event) override;

    // QObject interface
protected:
    void showEvent(QShowEvent *event) override;
    void paintEvent(QPaintEvent *event) override;
    void leaveEvent(QEvent *event) override;

private:
    Ui::GitLogPreviewWidget *ui;

    QTableWidget *m_instance;
    QTableWidget *m_tableWidget; // redirect to 'm_instance'
    GetColumnsFunc m_columnsFunc;

    QProcess *m_process; // git

};

#endif // GITLOGPREVIEWWIDGET_H
